package com.yoga.service.impl;

import com.yoga.dao.CoachMapper;
import com.yoga.dao.CourseMapper;
import com.yoga.dao.MemberMapper;
import com.yoga.dao.ReservationMapper;
import com.yoga.model.DashboardSummaryVO;
import com.yoga.service.DashboardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yan
 * @date 2025/8/18
 */
@Service
public class DashboardServiceImpl implements DashboardService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private CoachMapper coachMapper;

    @Autowired
    private ReservationMapper reservationMapper;

    @Override
    public DashboardSummaryVO getSummary() {
        DashboardSummaryVO vo = new DashboardSummaryVO();
        vo.setTotalMembers(Math.toIntExact(memberMapper.selectCount(null)));
        vo.setVipMembers(memberMapper.countVip());
        vo.setRegularMembers(memberMapper.countRegular());
        vo.setTotalCourses(Math.toIntExact(courseMapper.selectCount(null)));
        vo.setTotalCoaches(Math.toIntExact(coachMapper.selectCount(null)));
        vo.setTodayReservations(reservationMapper.countToday());
        return vo;
    }

    @Override
    public Map<String, Object> getCourseTrend() {
        List<Map<String, Object>> trendList = reservationMapper.getLast7DaysTrend();
        List<String> dates = new ArrayList<>();
        List<Integer> counts = new ArrayList<>();
        for (Map<String, Object> map : trendList) {
            dates.add(map.get("date").toString());
            counts.add(((Number) map.get("count")).intValue());
        }
        Map<String, Object> result = new HashMap<>();
        result.put("dates", dates);
        result.put("counts", counts);
        return result;
    }

    @Override
    public Map<String, Object> getAll() {
        List<Map<String,Object>> result = courseMapper.getAll();
        List<String> names = new ArrayList<>();
        List<Integer> counts = new ArrayList<>();
        for (Map<String, Object> map : result) {
            names.add(map.get("name").toString());
            counts.add(((Number) map.get("contain")).intValue());
        }
        Map<String, Object> collection = new HashMap<>();
        collection.put("names", names);
        collection.put("counts", counts);
        return collection;
    }
}

